package task02;

import java.util.Scanner;

public class Code0202 {
        private String[][] board;
        private char white;
        private char black;
        private int rows;
        private int cols;

        Code0202() {
        }

        Code0202(int rows, int cols, char white, char black) {
            setBoard(rows, cols);
            setWhite(white);
            setBlack(black);
            setRows(rows);
            setCols(cols);
        }

        public void setRows(int rows) {
            this.rows = rows;
        }

        public void setCols(int cols) {
            this.cols = cols;
        }

        public int getRows() {
            return rows;
        }

        public int getCols() {
            return cols;
        }

        public void setBoard(int rows, int cols) {
            board = new String[rows][cols];
        }

        public void setWhite(char white) {
            this.white = white;
        }

        public void setBlack(char black) {
            this.black = black;
        }

        public String[][] getBoard(int rows, int cols) {
            return board;
        }

        public char getWhite() {
            return white;
        }

        public char getBlack() {
            return black;
        }

        public void getBoard() {
            for (int i = 0; i < board.length; i++) {
                for (int j = 0; j < board[0].length; j++) {
                    if (i == 0) {
                        board[i][j] = String.format("%x", j - 1);
                    } else if (j == 0) {
                        board[i][j] = String.format("%x", i - 1);
                    } else {
                        board[i][j] = "+";
                    }
                }
            }
            board[0][0] = " ";
        }

        public void showBoard() {
            for (String[] str : board) {
                for (int j = 0; j < board[0].length; j++) {
                    if ("白".equals(str[j])) {
                        System.out.print(white + " ");
                    } else if ("黑".equals(str[j])) {
                        System.out.print(black + " ");
                    } else {
                        System.out.print(str[j] + " ");

                    }
                }
                System.out.println();
            }
        }

        private Boolean isWin(int x, int y, String chess, int rows, int cols) {
            int countCol = -1;
            int countRow = -1;
            int countDiagonalLeft = -1;
            int countDiagonalRight = -1;
            int a;
            int b;
            a = x;
            b = y;
            while (true) {
                if (chess.equals(board[a][b])) {
                    countRow++;
                    if (b == 0) {
                        break;
                    }
                    b--;
                } else {
                    break;
                }
            }
            a = x;
            b = y;
            while (true) {
                if (chess.equals(board[a][b])) {
                    countRow++;
                    if (b == (cols - 1)) {
                        break;
                    }
                    b++;
                } else {
                    break;
                }
            }

            a = x;
            b = y;
            while (true) {
                if (chess.equals(board[a][b])) {
                    countCol++;
                    if (a == 0) {
                        break;
                    }
                    a--;
                } else {
                    break;
                }
            }
            a = x;
            b = y;
            while (true) {
                if (chess.equals(board[a][b])) {
                    countCol++;
                    if (a == (rows - 1)) {
                        break;
                    }
                    a++;
                } else {
                    break;
                }
            }
            a = x;
            b = y;
            while (true) {
                if (chess.equals(board[a][b])) {
                    countDiagonalLeft++;
                    if (a == 0 || b == 0) {
                        break;
                    }
                    a--;
                    b--;
                } else {
                    break;
                }
            }
            a = x;
            b = y;
            while (true) {
                if (chess.equals(board[a][b])) {
                    countDiagonalLeft++;
                    if (a == (rows - 1) || b == (cols - 1)) {
                        break;
                    }
                    a++;
                    b++;
                } else {
                    break;
                }
            }
            a = x;
            b = y;
            while (true) {
                if (chess.equals(board[a][b])) {
                    countDiagonalRight++;
                    if (a == 0 || b == (cols - 1)) {
                        break;
                    }
                    a--;
                    b++;
                } else {
                    break;
                }
            }
            a = x;
            b = y;
            while (true) {
                if (chess.equals(board[a][b])) {
                    countDiagonalRight++;
                    if (a == (rows - 1) || b == 0) {
                        break;
                    }
                    a++;
                    b--;
                } else {
                    break;
                }
            }

            System.out.println("countRow:" + countRow);
            System.out.println("countCol:" + countCol);
            System.out.println("countDiagonalLeft:" + countDiagonalLeft);
            System.out.println("countDiagonalRight:" + countDiagonalRight);

            return (countRow >= 5 || countCol >= 5 || countDiagonalLeft >= 5 || countDiagonalRight >= 5);
        }

        public void startGame() {
            Scanner sc = new Scanner(System.in);
            boolean flag = true;
            int x;
            int y;
            String chess;
            while (true) {
                if (flag) {
                    System.out.println("请白方落子。");
                    chess = "白";
                } else {
                    System.out.println("请黑方落子。");
                    chess = "黑";
                }
                x = sc.nextInt() + 1;
                y = sc.nextInt() + 1;
                if (x < 0 || y < 0 || x > 16 || y > 16) {
                    System.out.println("落子超出棋盘范围，请重新落子。");
                    continue;
                }
                if ("+".equals(board[x][y])) {
                    board[x][y] = chess;
                } else {
                    System.out.println("当前坐标不可落子，请重新落子。");
                    continue;
                }
                showBoard();
                flag = !flag;
                boolean isWin = isWin(x, y, chess, getRows(), getCols());
                if (isWin) {
                    System.out.printf("恭喜%s方获胜！", chess);
                    break;
                }
            }
            sc.close();
        }
    }
